import axios from 'axios'
import store from '@/store'
import router from '@/router'
import { setToken, getToken, removeToken } from "@/libs/auth";
import { Notification, MessageBox, Message, Loading } from 'element-ui'

// 创建Axios实例
const axiosInstance = axios.create({
    baseURL: process.env.VUE_APP_BASE_SERVICE_URL, // API的base_url
    timeout: 30000, // 请求超时时间
    withCredentials: true // 跨域请求，允许保存cookie
})

// request拦截器
axiosInstance.interceptors.request.use(config => {
    // 发送请求前
    config.headers['Auth-Token'] = getToken() // 设置默认携带的头信息(Token)
    return config
}, error => {
    // 请求错误
    console.log('request err: ' + error)
    return Promise.reject(error);
})

// response拦截器
axiosInstance.interceptors.response.use(
    response => {
        // 响应结果返回到页面前可以对数据进行处理，自定义code来标示请求状态，当code返回如下情况为权限有问题，登出并返回到登录页
        const data = response.data

        if (data.status === 412) { //Token已过期
            store.dispatch('FedLogOut').then(() => {
                MessageBox.alert(data.msg, '警告', {
                    confirmButtonText: '重新登陆',
                    callback: () => {
                        Loading.service({
                            lock: true,
                            text: '正在退出，请稍等...',
                            background: 'rgba(0, 0, 0, 0.7)'
                        });
                        setTimeout(() => {
                            window.location.reload()
                        }, 500)

                    }
                })
            })
            return response
        }

        return response
    },
    error => {
        // 请求已发出，但服务器响应的状态码不在 2xx 范围内
        if (error.response) {
            switch (error.response.status) {
                case 0:
                    Msg('error', error.response.status + '网络暂时未能连通')
                    break
                case 400:
                    Msg('error', error.response.status + '错误的请求')
                    break
                case 412:
                    /* new MessageBox('未登录或登录超时，请重新登录', '提示', {
                        confirmButtonText: '重新登录',
                        type: 'warning'
                    }).then(() => {
                        location.reload()
                    }).catch(() => {
                        console.log('登出发生错误')
                    });
 */
                    break
                case 401:
                case 403:
                    Msg('error', error.response.status + '  ' + error.response.data.message)
                    break
                case 404:
                    ExceptionMsg('info', error.response.status, '该资源不存在：' + error.config.url)
                    break
                case 405:
                    Msg('error', error.response.status + '请求的方式不正确')
                    break
                case 408:
                    Msg('error', error.response.status + '请求超时')
                    break
                case 500:
                    ExceptionMsg('', '服务器异常-' + error.response.status, error.response.data.message)
                    break
                case 503:
                    Msg('error', error.response.status + ' ' + error.response.data.message)
                    break
                case 504:
                    Msg('error', error.response.status + '服务器被吃了⊙﹏⊙∥')
                    break
                default:
                    break
            }
        }
        /* else {
            console.log('请求发生错误==》', error)
            Msg('error', '未知错误!')
        }*/

        return Promise.reject(error)
    }
)

function Msg(type, message) {
    Message({
        type: type,
        duration: 2000,
        message: message
    })
}

function NotificationMsg(type, title, message) {
    Notification({
        type: type,
        duration: 2000,
        title: title,
        message: message
    })
}

function ExceptionMsg(type, title, message) {
    MessageBox({
        type: type,
        title: title,
        message: message,
        customClass: 'errorMessageBox'
    })
}

export default axiosInstance;